/*     */ package com.zimbra.cs.account.accesscontrol;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.accesscontrol.generated.RightConsts;
/*     */ import java.io.PrintStream;
/*     */ import java.util.Arrays;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class Right
/*     */   extends RightConsts
/*     */   implements Comparable<Right>
/*     */ {
/*     */   private static final int NOT_CACHEABLE = -1;
/*     */   
/*     */   public static enum RightType
/*     */   {
/*  28 */     preset, 
/*  29 */     getAttrs, 
/*  30 */     setAttrs, 
/*  31 */     combo;
/*     */     
/*     */     private RightType() {}
/*     */     
/*  35 */     public static RightType fromString(String s) throws ServiceException { try { return valueOf(s);
/*     */       } catch (IllegalArgumentException e) {
/*  37 */         throw ServiceException.PARSE_ERROR("unknown right type: " + s, e);
/*     */       }
/*     */     }
/*     */     
/*     */     public boolean isUserDefinable() {
/*  42 */       return this != preset;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*  47 */   private static int sMaxCacheIndex = 0;
/*     */   
/*     */   private final String mName;
/*     */   protected RightType mRightType;
/*     */   private String mDesc;
/*     */   private Help mHelp;
/*     */   private UI mUI;
/*     */   private Boolean mDefault;
/*     */   protected TargetType mTargetType;
/*     */   protected TargetType mGrantTargetType;
/*     */   private CheckRightFallback mFallback;
/*  58 */   int mCacheIndex = -1;
/*     */   
/*     */   static void init(RightManager rm) throws ServiceException {
/*  61 */     UserRight.init(rm);
/*  62 */     AdminRight.init(rm);
/*     */   }
/*     */   
/*     */   Right(String name, RightType rightType) {
/*  66 */     this.mRightType = rightType;
/*  67 */     this.mName = name;
/*     */   }
/*     */   
/*     */   String dump(StringBuilder sb) {
/*  71 */     if (sb == null) {
/*  72 */       sb = new StringBuilder();
/*     */     }
/*  74 */     sb.append("name         = " + this.mName + "\n");
/*  75 */     sb.append("type         = " + this.mRightType.name() + "\n");
/*  76 */     sb.append("desc         = " + this.mDesc + "\n");
/*  77 */     sb.append("help         = " + (this.mHelp == null ? "null" : this.mHelp.getName()) + "\n");
/*  78 */     sb.append("ui           = " + (this.mUI == null ? "null" : this.mUI.getDesc()) + "\n");
/*  79 */     sb.append("default      = " + this.mDefault + "\n");
/*  80 */     sb.append("target Type  = " + this.mTargetType + "\n");
/*     */     
/*  82 */     return sb.toString();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public int compareTo(Right other)
/*     */   {
/*  90 */     return this.mName.compareTo(other.mName);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   abstract boolean overlaps(Right paramRight)
/*     */     throws ServiceException;
/*     */   
/*     */ 
/*     */ 
/*     */   public boolean isUserRight()
/*     */   {
/* 102 */     return false;
/*     */   }
/*     */   
/*     */   public boolean isPresetRight() {
/* 106 */     return false;
/*     */   }
/*     */   
/*     */   public boolean isAttrRight() {
/* 110 */     return false;
/*     */   }
/*     */   
/*     */   public boolean isComboRight() {
/* 114 */     return false;
/*     */   }
/*     */   
/*     */   public RightType getRightType() {
/* 118 */     return this.mRightType;
/*     */   }
/*     */   
/*     */   public RightClass getRightClass() {
/* 122 */     return isUserRight() ? RightClass.USER : RightClass.ADMIN;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public String getName()
/*     */   {
/* 133 */     return this.mName;
/*     */   }
/*     */   
/*     */   public String getDesc() {
/* 137 */     return this.mDesc;
/*     */   }
/*     */   
/*     */   void setDesc(String desc) {
/* 141 */     this.mDesc = desc;
/*     */   }
/*     */   
/*     */   public Help getHelp() {
/* 145 */     return this.mHelp;
/*     */   }
/*     */   
/*     */   public UI getUI() {
/* 149 */     return this.mUI;
/*     */   }
/*     */   
/*     */   void setHelp(Help help) {
/* 153 */     this.mHelp = help;
/*     */   }
/*     */   
/*     */   void setUI(UI ui) {
/* 157 */     this.mUI = ui;
/*     */   }
/*     */   
/*     */   public Boolean getDefault() {
/* 161 */     return this.mDefault;
/*     */   }
/*     */   
/*     */   void setDefault(Boolean defaultValue) {
/* 165 */     this.mDefault = defaultValue;
/*     */   }
/*     */   
/*     */   public CheckRightFallback getFallback() {
/* 169 */     return this.mFallback;
/*     */   }
/*     */   
/*     */   void setFallback(CheckRightFallback fallback) {
/* 173 */     this.mFallback = fallback;
/*     */   }
/*     */   
/*     */   boolean executableOnTargetType(TargetType targetType) {
/* 177 */     return this.mTargetType == targetType;
/*     */   }
/*     */   
/*     */   boolean isValidTargetForCustomDynamicGroup() {
/* 181 */     return this.mTargetType == TargetType.group;
/*     */   }
/*     */   
/*     */ 
/*     */   abstract boolean grantableOnTargetType(TargetType paramTargetType);
/*     */   
/*     */ 
/*     */   abstract Set<TargetType> getGrantableTargetTypes();
/*     */   
/*     */ 
/*     */   boolean allowSubDomainModifier()
/*     */   {
/* 193 */     return executableOnTargetType(TargetType.domain);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   boolean allowDisinheritSubGroupsModifier()
/*     */   {
/* 201 */     return (executableOnTargetType(TargetType.dl)) || (executableOnTargetType(TargetType.account)) || (executableOnTargetType(TargetType.calresource));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   boolean isTheSameRight(Right other)
/*     */   {
/* 210 */     return this == other;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   final String reportGrantableTargetTypes()
/*     */   {
/* 217 */     Set<TargetType> targetTypes = getGrantableTargetTypes();
/* 218 */     StringBuilder sb = new StringBuilder();
/* 219 */     boolean first = true;
/* 220 */     for (TargetType tt : targetTypes) {
/* 221 */       if (!first) {
/* 222 */         sb.append(" or ");
/*     */       } else {
/* 224 */         first = false;
/*     */       }
/* 226 */       sb.append(tt.getCode());
/*     */     }
/* 228 */     return sb.toString();
/*     */   }
/*     */   
/*     */   void setTargetType(TargetType targetType) throws ServiceException {
/* 232 */     if (this.mTargetType != null) {
/* 233 */       throw ServiceException.PARSE_ERROR("target type already set", null);
/*     */     }
/*     */     
/* 236 */     this.mTargetType = targetType;
/*     */   }
/*     */   
/*     */   void setGrantTargetType(TargetType targetType) throws ServiceException {
/* 240 */     if (!isUserRight()) {
/* 241 */       throw ServiceException.PARSE_ERROR("grant target type is only supported on user rights", null);
/*     */     }
/*     */     
/* 244 */     if (!grantableOnTargetType(targetType)) {
/* 245 */       throw ServiceException.PARSE_ERROR(String.format("invalid grant target type: %s, valid grant target type for right %s are: %s", new Object[] { targetType.getCode(), getName(), Arrays.deepToString(getGrantableTargetTypes().toArray()) }), null);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 252 */     this.mGrantTargetType = targetType;
/*     */   }
/*     */   
/*     */   void verifyTargetType() throws ServiceException {
/* 256 */     if (this.mTargetType == null) {
/* 257 */       throw ServiceException.PARSE_ERROR("missing target type", null);
/*     */     }
/*     */   }
/*     */   
/*     */   public TargetType getTargetType() throws ServiceException {
/* 262 */     return this.mTargetType;
/*     */   }
/*     */   
/*     */   public TargetType getGrantTargetType() {
/* 266 */     return this.mGrantTargetType;
/*     */   }
/*     */   
/*     */   public String getTargetTypeStr()
/*     */   {
/* 271 */     return this.mTargetType.getCode();
/*     */   }
/*     */   
/*     */   public String getGrantTargetTypeStr() {
/* 275 */     if (this.mGrantTargetType == null) {
/* 276 */       return null;
/*     */     }
/* 278 */     return this.mGrantTargetType.getCode();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   void completeRight()
/*     */     throws ServiceException
/*     */   {
/* 292 */     if (getDesc() == null)
/* 293 */       throw ServiceException.PARSE_ERROR("missing description", null);
/* 294 */     verifyTargetType();
/*     */   }
/*     */   
/*     */   void setCacheable() {
/* 298 */     this.mCacheIndex = getNextCacheIndex();
/*     */   }
/*     */   
/*     */   boolean isCacheable() {
/* 302 */     return this.mCacheIndex != -1;
/*     */   }
/*     */   
/*     */   public int getCacheIndex() {
/* 306 */     return this.mCacheIndex;
/*     */   }
/*     */   
/*     */   private static synchronized int getNextCacheIndex() {
/* 310 */     sMaxCacheIndex += 1;
/* 311 */     return sMaxCacheIndex - 1;
/*     */   }
/*     */   
/*     */   public static int getMaxCacheIndex() {
/* 315 */     return sMaxCacheIndex;
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws ServiceException
/*     */   {
/* 320 */     RightManager.getInstance();
/*     */     
/* 322 */     Right r1 = Rights.Admin.R_domainAdminAccountRights;
/* 323 */     Right r2 = Rights.Admin.R_domainAdminRights;
/* 324 */     boolean overlaps = r1.overlaps(r2);
/* 325 */     System.out.println(r1.getName() + " " + r2.getName() + " => " + overlaps);
/*     */     
/* 327 */     r1 = Rights.Admin.R_modifyCos;
/* 328 */     overlaps = r1.overlaps(r2);
/* 329 */     System.out.println(r1.getName() + " " + r2.getName() + " => " + overlaps);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/accesscontrol/Right.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */